package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CollectKeyBean;
import cn.bocweb.spinning.model.Bean.MyTestCollectBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.MyTestCollectActivity;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/1/25.
 */
public class MyTestCollectFragment extends Fragment implements View.OnClickListener {


    // TODO: 16/3/1 记得所有考试系统都要管理isvisible. 分别在获得题目,点击监听.和获得答案都管理一下

    MyTestCollectBean.DataEntity dataEntity;
    UserPresenter presenter;
    String userid, libid;
    String ttype;
    String tid;
    Map<Integer, Boolean> map;
    int position, right;
    boolean isVisible = false, isright = false, isCheckExplain, isSelect = false;
    CollectKeyBean.DataEntity testKeys;
    @Bind(R.id.train_type)
    TextView trainType;
    @Bind(R.id.train_title)
    TextView trainTitle;
    @Bind(R.id.collect_title_layout)
    LinearLayout collectTitleLayout;
    @Bind(R.id.train_select_icon_a)
    ImageView trainSelectIconA;
    @Bind(R.id.train_select_answer_a)
    TextView trainSelectAnswerA;
    @Bind(R.id.train_select_a)
    LinearLayout trainSelectA;
    @Bind(R.id.train_select_icon_b)
    ImageView trainSelectIconB;
    @Bind(R.id.train_select_answer_b)
    TextView trainSelectAnswerB;
    @Bind(R.id.train_select_b)
    LinearLayout trainSelectB;
    @Bind(R.id.train_select_icon_c)
    ImageView trainSelectIconC;
    @Bind(R.id.train_select_answer_c)
    TextView trainSelectAnswerC;
    @Bind(R.id.train_select_c)
    LinearLayout trainSelectC;
    @Bind(R.id.train_select_icon_d)
    ImageView trainSelectIconD;
    @Bind(R.id.train_select_answer_d)
    TextView trainSelectAnswerD;
    @Bind(R.id.train_select_d)
    LinearLayout trainSelectD;
    @Bind(R.id.train_select)
    LinearLayout trainSelect;
    @Bind(R.id.train_judge_icon_a)
    ImageView trainJudgeIconA;
    @Bind(R.id.train_judge_answer_a)
    TextView trainJudgeAnswerA;
    @Bind(R.id.train_judge_a)
    LinearLayout trainJudgeA;
    @Bind(R.id.train_judge_icon_b)
    ImageView trainJudgeIconB;
    @Bind(R.id.train_judge_answer_b)
    TextView trainJudgeAnswerB;
    @Bind(R.id.train_judge_b)
    LinearLayout trainJudgeB;
    @Bind(R.id.train_judge)
    LinearLayout trainJudge;
    @Bind(R.id.train_allrate)
    TextView trainAllrate;
    @Bind(R.id.sequence_wrong_people)
    TextView sequenceWrongPeople;
    @Bind(R.id.train_personrate)
    TextView trainPersonrate;
    @Bind(R.id.sequence_wrong_my_wrong)
    TextView sequenceWrongMyWrong;
    @Bind(R.id.sequence_wrong_my_right)
    TextView sequenceWrongMyRight;
    @Bind(R.id.train_explain_rate)
    LinearLayout trainExplainRate;
    @Bind(R.id.train_explain)
    TextView trainExplain;
    @Bind(R.id.train_explain_detail)
    LinearLayout trainExplainDetail;
    @Bind(R.id.sequence_wrong_layout)
    LinearLayout sequenceWrongLayout;
    @Bind(R.id.sequence_explain_explain)
    TextView sequenceExplainExplain;
    @Bind(R.id.sequence_explain_detail)
    LinearLayout sequenceExplainDetail;
    @Bind(R.id.sequence_explain_allrate)
    TextView sequenceExplainAllrate;
    @Bind(R.id.sequence_explain_people_wrong)
    TextView sequenceExplainPeopleWrong;
    @Bind(R.id.sequence_explain_personrate)
    TextView sequenceExplainPersonrate;
    @Bind(R.id.sequence_explain_my_wrong)
    TextView sequenceExplainMyWrong;
    @Bind(R.id.sequence_explain_my_right)
    TextView sequenceExplainMyRight;
    @Bind(R.id.sequence_explain_rate)
    LinearLayout sequenceExplainRate;
    @Bind(R.id.sequence_explain_layout)
    LinearLayout sequenceExplainLayout;

    String allRate, personRate, myRight, myWrong, explain;
    MyTestCollectActivity myTestCollectActivity;
    Timer timer;
    Timer timer1;

    public MyTestCollectBean.DataEntity getDataEntity() {
        return dataEntity;
    }

    public void setDataEntity(MyTestCollectBean.DataEntity dataEntity) {
        this.dataEntity = dataEntity;

    }

    public void setPosition(int position) {
        this.position = position;
    }

    public MyTestCollectFragment(int position, String userid, String libid) {
        this.position = position;
        this.userid = userid;
        this.libid = libid;
        map = new HashMap<>();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timer1 = new Timer();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my_test_collect, container, false);
        ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        presenter = new UserPresenterImp();
        myTestCollectActivity = (MyTestCollectActivity) getActivity();

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initClickBtn() {
        trainSelectA.setOnClickListener(this);
        trainSelectB.setOnClickListener(this);
        trainSelectC.setOnClickListener(this);
        trainSelectD.setOnClickListener(this);
        trainJudgeA.setOnClickListener(this);
        trainJudgeB.setOnClickListener(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisible) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    presenter.collectQuestion(position, userid, libid);
                }
            };

            if (timer1 != null) {
                timer1.schedule(timerTask, 1000);
            } else {
                timer = new Timer();
                timer1.schedule(timerTask, 1000);
            }
        } else {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        DeviceUtil.Log("hidden = " + hidden);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (timer != null) {
            timer.cancel();
            timer.purge();
        }
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(MyTestCollectBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                dataEntity = bean.getData();
                allRate = bean.getData().getAllrate();
                personRate = bean.getData().getPersonrate();
                myRight = bean.getData().getOk_times();
                myWrong = bean.getData().getWrong_times();
                explain = bean.getData().getExplain();
                ttype = bean.getData().getTtype();
                tid = bean.getData().getId();
                initType();
                initTitle();
                initOption();

                if (isSelect) {
                    if (bean.getData().getTtype().equals("1")) {
                        initSelectAnswer1();
                    } else {
                        initJudgeAnswer1();
                    }
                    if (!isCheckExplain) {
                        trainExplain.setText(explain);
                        trainAllrate.setText(allRate + "%");
                        trainPersonrate.setText(personRate + "%");
                        sequenceWrongPeople.setText(allRate + "%");
                        sequenceWrongMyRight.setText(myRight);
                        sequenceWrongMyWrong.setText(myWrong);

                        sequenceWrongLayout.setVisibility(View.VISIBLE);
                        sequenceExplainLayout.setVisibility(View.GONE);
                    } else {
                        sequenceExplainAllrate.setText(allRate + "%");
                        sequenceExplainExplain.setText(explain);
                        sequenceExplainMyRight.setText(myRight);
                        sequenceExplainMyWrong.setText(myWrong);
                        sequenceExplainPersonrate.setText(personRate + "%");
                        sequenceExplainPeopleWrong.setText(allRate + "%");

                        sequenceExplainLayout.setVisibility(View.VISIBLE);
                        sequenceWrongLayout.setVisibility(View.GONE);
                    }
                } else {
                    initClickBtn();
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }

            isVisible = !isVisible;
        }
    }

    private void initJudgeAnswer1() {
        if (isright) {
            switch (right) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                }
            }
        } else {
            switch (right) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 2: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        }
    }

    private void initSelectAnswer1() {
        if (isright) {
            switch (right) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 4: {
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            switch (right) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 2: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 3: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 4: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        }
    }


    public void onEventMainThread(CollectKeyBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                allRate = bean.getData().getAllrate();
                personRate = bean.getData().getPersonrate();
                myRight = bean.getData().getOk_times();
                myWrong = bean.getData().getWrong_times();
                explain = bean.getData().getExplain();

                trainExplainRate.setVisibility(View.VISIBLE);
                trainExplainDetail.setVisibility(View.VISIBLE);

                trainAllrate.setText(allRate + "%");
                trainPersonrate.setText(personRate + "%");
                trainExplain.setText(explain);
                testKeys = bean.getData();

                sequenceWrongPeople.setText(allRate + "%");
                sequenceWrongMyRight.setText(myRight);
                sequenceWrongMyWrong.setText(myWrong);

                sequenceExplainLayout.setVisibility(View.GONE);
                sequenceWrongLayout.setVisibility(View.VISIBLE);

            }

            isVisible = !isVisible;
        }
    }

    private void initOption() {
        if (!(dataEntity.getOption() == null || dataEntity.getOption().isEmpty())) {
            trainSelectAnswerA.setText(dataEntity.getOption().get(0));
            trainSelectAnswerB.setText(dataEntity.getOption().get(1));
            trainSelectAnswerC.setText(dataEntity.getOption().get(2));
            trainSelectAnswerD.setText(dataEntity.getOption().get(3));
        }
    }

    private void initInfo(int choose) {
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);

        int type = Integer.parseInt(dataEntity.getTtype());
        right = Integer.parseInt(dataEntity.getRight());

        isSelect = true;

        if (right == choose) {
            isright = true;
            presenter.CollectKey(String.valueOf(position + 1), userid, libid, String.valueOf(choose), "1");
            myTestCollectActivity.setAnswerMap(position, isright);

        } else {
            isright = false;
            presenter.CollectKey(String.valueOf(position + 1), userid, libid, String.valueOf(choose), "0");
            myTestCollectActivity.setAnswerMap(position, isright);
        }

        switch (type) {
            case 1: {
                if (right == choose) {
                    switch (right) {
                        case 1: {
                            trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 3: {
                            trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 4: {
                            trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    switch (right) {
                        case 1: {
                            initAnswer(trainSelectIconA, false);
                            break;
                        }
                        case 2: {
                            initAnswer(trainSelectIconB, false);
                            break;
                        }
                        case 3: {
                            initAnswer(trainSelectIconC, false);
                            break;
                        }
                        case 4: {
                            initAnswer(trainSelectIconD, false);
                            break;
                        }
                    }
                }
                break;
            }
            case 2: {
                if (choose == right) {
                    switch (right) {
                        case 1: {
                            trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    switch (right) {
                        case 1: {
                            initJudgeAnswer(trainJudgeIconA);
                            break;
                        }
                        case 2: {
                            initJudgeAnswer(trainJudgeIconB);
                            break;
                        }
                    }
                }
            }
        }
    }

    private void initAnswer(ImageView icon, boolean isright) {
        if (!isright) {
            trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
        }

        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    private void initJudgeAnswer(ImageView icon) {
        trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
        trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);

        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    private void initType() {
        trainType.setVisibility(View.VISIBLE);
        String ttype = dataEntity.getTtype();
        if (!(ttype == null || ttype.isEmpty())) {
            switch (Integer.parseInt(ttype)) {
                case 1: {
                    trainType.setText("选");
                    trainSelect.setVisibility(View.VISIBLE);
                    trainJudge.setVisibility(View.GONE);
                    break;
                }
                case 2: {
                    trainType.setText("判");
                    trainJudge.setVisibility(View.VISIBLE);
                    trainSelect.setVisibility(View.GONE);
                    break;
                }
            }
        }
    }

    private void initTitle() {
        trainTitle.setText(dataEntity.getTitle());

        collectTitleLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        isVisible = false;
    }

    @Override
    public void onClick(View v) {
        isVisible = !isVisible;
        switch (v.getId()) {
            case R.id.train_select_a: {
                initInfo(1);
                break;
            }
            case R.id.train_select_b: {
                initInfo(2);
                break;
            }
            case R.id.train_select_c: {
                initInfo(3);
                break;
            }
            case R.id.train_select_d: {
                initInfo(4);
                break;
            }
            case R.id.train_judge_a: {
                initInfo(1);
                break;
            }
            case R.id.train_judge_b: {
                initInfo(2);
                break;
            }
        }
    }

    public void showExplain() {
        sequenceExplainAllrate.setText(allRate + "%");
        sequenceExplainExplain.setText(explain);
        sequenceExplainMyRight.setText(myRight);
        sequenceExplainMyWrong.setText(myWrong);
        sequenceExplainPersonrate.setText(personRate + "%");
        sequenceExplainPeopleWrong.setText(allRate + "%");

        sequenceWrongLayout.setVisibility(View.GONE);
        sequenceExplainLayout.setVisibility(View.VISIBLE);

        isCheckExplain = !isCheckExplain;
    }

    public String getTtype() {
        return ttype;
    }

    public String getTid() {
        return tid;
    }

    public void deleteMap() {
        map.remove(position);
    }
}
